<?php

namespace Admin\Controller;
use Org\Util\Sync;
use Org\Util;
use Think\Model;

/**
 * Created by PhpStorm.
 * User: Nico
 * Date: 2016/7/21
 * Time: 9:51
 */
class AssisterController extends AdminController
{
    /**
     * 订阅项目配置 列表
     * User:Nico
     */
    public function notifications_index(){
        $title = I('title');
        $map['delflag']  =   0;
        $map['title'] = array('like', '%' . (string) $title . '%');
        $list = $this->lists('assister_notifications', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }
    /**
     * 订阅项目配置 新增
     * User:Nico
     */
    public function notifications_add($title = "",$remark="") {
        if (IS_POST) {
            $data = array('title' => $title,'remark' => $remark,'createAt' => NOW_TIME, 'updateAt' => NOW_TIME, 'createdUser' => UID, 'updatedUser' => UID);
            $id = M('assister_notifications')->add($data);
            if (!$id) {
                $this->error("添加失败！");
            } else {
                $this->success('添加成功！', U('notifications_index'));
            }
        } else {
            $this->display();
        }
    }
    /**
     * 订阅项目配置 编辑
     * User:Nico
     */
    public function notifications_edit($title = "",$remark="") {
        $id = I('id');
        if (IS_POST) {
            $data = array('title' => $title, 'remark' => $remark,'updateAt' => NOW_TIME, 'updatedUser' => UID);
            if (!M('assister_notifications')->where(array('id' => $id))->save($data)) {
                $this->error("修改失败！");
            } else {
                $this->success('修改成功！', U('notifications_index'));
            }
        } else {
            $data = M('assister_notifications')->where(array('id' => $id))->find();
            $this->data = $data;
            $this->display();
        }
    }
    /**
     * 订阅项目配置 删除
     * User:Nico
     */
    public function notifications_del($id = 0) {
        if ($id) {
            $classify = M("assister_notifications");
            $data = array('delflag'=>1,'updateAt' => NOW_TIME, 'updatedUser' => UID);
            $result = $classify-> where(array('id'=>$id))->setField($data);
            if($result){
                $this->success('删除成功！', U('notifications_index'));
            }else{
                $this->error("删除失败！");
            }
        }
    }

    /**
     * 订阅项目配置 列表
     * User:Nico
     */
    public function pushtemp_index(){
        $title = I('title');
        $map['delflag']  =   0;
        $map['title'] = array('like', '%' . (string) $title . '%');
        $list = $this->lists('assister_pushtemp', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }
    /**
     * 订阅项目配置 新增
     * User:Nico
     */
    public function pushtemp_add($title = "",$intro='',$notification_id='',$target_user='',$content='',$url='') {
        if (IS_POST) {
            $data = array(
                'title' => $title,
                'intro' => $intro,
                'notification_id' => $notification_id,
                'target_user' => $target_user,
                'content' => $content,
                'url' => $url,
                'createAt' => NOW_TIME,
                'updateAt' => NOW_TIME);
            $id = M('assister_pushtemp')->add($data);
            if (!$id) {
                $this->error("添加失败！");
            } else {
                $this->success('添加成功！', U('pushtemp_index'));
            }
        } else {
            $this->display();
        }
    }
    /**
     * 订阅项目配置 编辑
     * User:Nico
     */
    public function pushtemp_edit($title = "",$intro='',$notification_id='',$target_user='',$content='',$url='') {
        $id = I('id');
        if (IS_POST) {
            $data = array(
                'title' => $title,
                'intro' => $intro,
                'notification_id' => $notification_id,
                'target_user' => $target_user,
                'content' => $content,
                'url' => $url,
                'updateAt' => NOW_TIME);
            if (!M('assister_pushtemp')->where(array('id' => $id))->save($data)) {
                $this->error("修改失败！");
            } else {
                $this->success('修改成功！', U('pushtemp_index'));
            }
        } else {
            $data = M('assister_pushtemp')->where(array('id' => $id))->find();
            $this->data = $data;
            $this->display();
        }
    }
    /**
     * 订阅项目配置 删除
     * User:Nico
     */
    public function pushtemp_del($id = 0) {
        if ($id) {
            $classify = M("assister_pushtemp");
            $data = array('delflag'=>1,'updateAt' => NOW_TIME);
            $result = $classify-> where(array('id'=>$id))->setField($data);
            if($result){
                $this->success('删除成功！', U('pushtemp_index'));
            }else{
                $this->error("删除失败！");
            }
        }
    }

    /**
     * 推送流水记录
     * TODO
     * User:Nico
     */
    public function pushflow_index() {
        $title = I('title');
        $map['delflag']  =   0;
        $map['title'] = array('like', '%' . (string) $title . '%');
        $list = $this->lists('assister_pushtemp', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }

    public function apppush_ajax($pushuser="",$pushtags="",$pushalias="",$pushcontent=""){
        if ($pushuser==1){
            $AEHttp=new Util\AEHttp("api");
            $param['content']=$pushcontent;
            $result=$AEHttp->postapi(".erppush.boardcast",$param);
            if($result['sendno']){
                $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('error');
            }
        }
        if ($pushuser==2){
            $AEHttp=new Util\AEHttp("api");
            $param['tags']=$pushtags;
            $param['content']=$pushcontent;
            $result=$AEHttp->postapi(".erppush.notify",$param);
            if($result['sendno']){
                $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('error');
            }
        }
        if ($pushuser==3){
            $AEHttp=new Util\AEHttp("api");
            $param['alias']=$pushalias;
            $param['content']=$pushcontent;
            $result=$AEHttp->postapi(".erppush.notify",$param);
            if($result['sendno']){
                $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('error');
            }
        }
    }
}